//
//  LoginViewController.m
//  TeamInformatization
//
//  Created by bigger on 2019/3/31.
//  Copyright © 2019年 SPIC. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginMessage.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(transformView:) name:UIKeyboardWillChangeFrameNotification object:nil];//键盘的弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    // UI
    [self loadShowViews];
}

+ (void)userDidLogin:(void(^)(void))complte
{
    NSLog(@"修改一下嘛");
    complte();
}

+ (void)presentLoginVCWithCallBack:(void (^)(BOOL))callBack
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [loginVC setCallBack:callBack];
}

- (void)loadShowViews
{
    //背景图
    UIImageView *backgroundImg = [[UIImageView alloc] init];
    backgroundImg.image = [UIImage imageNamed:@"Bitmap"];
    [self.view addSubview:backgroundImg];
    backgroundImg.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    //logo图
    UIImageView *titleHeadImg = [[UIImageView alloc] init];
    titleHeadImg.image = [UIImage imageNamed:@"login_logo"];
    [self.view addSubview:titleHeadImg];
    [titleHeadImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(79);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(271, 145));
    }];
    //文字背景色view
    UIView *textv = [[UIView alloc] init];
    textv.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.7].CGColor;
    textv.layer.cornerRadius = 4;
    [self.view addSubview:textv];
    [textv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleHeadImg.mas_bottom).offset(39);
        make.left.mas_equalTo(30);
        make.height.mas_equalTo(88);
        make.right.mas_equalTo(-30);
    }];
    
    //用户image
    UIImageView *nameImg = [[UIImageView alloc] init];
    nameImg.image = [UIImage imageNamed:@"login_name"];
    [self.view addSubview:nameImg];
    [nameImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textv.mas_top).offset(13);
        make.left.equalTo(textv.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(16, 19));
    }];
    
    //nametext
    UITextField *nametext = [[UITextField alloc] init];
    nametext.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入用户名" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0],
                                        NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:12]
                                        }];
    nametext.attributedPlaceholder = attrString;
    [textv addSubview:nametext];
    [nametext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(textv.mas_top).offset(4);
        make.left.equalTo(nameImg.mas_right).offset(9);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
    }];
    
    //线
    UIView *linev = [[UIView alloc] init];
    linev.layer.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
    [self.view addSubview:linev];
    [linev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameImg.mas_bottom).offset(12);
        make.left.equalTo(textv.mas_left).offset(0);
        make.centerX.mas_equalTo(textv);
        make.height.mas_equalTo(1);
    }];
    
    //密码image
    UIImageView *pswImg = [[UIImageView alloc] init];
    pswImg.image = [UIImage imageNamed:@"login_psw"];
    [self.view addSubview:pswImg];
    [pswImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(linev.mas_bottom).offset(13);
        make.left.equalTo(textv.mas_left).offset(10);
        make.size.mas_equalTo(CGSizeMake(16, 18));
    }];
    
    //nametext
    UITextField *pswtext = [[UITextField alloc] init];
    pswtext.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    // 就下面这两行是重点
    NSAttributedString *attrString2 = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0],
                                        NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:12]
                                        }];
    pswtext.attributedPlaceholder = attrString2;
    [textv addSubview:pswtext];
    [pswtext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(linev.mas_bottom).offset(3);
        make.left.equalTo(pswImg.mas_right).offset(9);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
    }];
    
    //登录btn
    UIButton *loginBtn = [[UIButton alloc] init];
    loginBtn.layer.backgroundColor = [UIColor colorWithRed:204/255.0 green:51/255.0 blue:51/255.0 alpha:1.0].CGColor;
    loginBtn.layer.cornerRadius = 4;
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    loginBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17];
    loginBtn.titleLabel.textColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    [loginBtn addTarget:self action:@selector(loginBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.equalTo(textv.mas_bottom).offset(30);
        make.height.mas_equalTo(44);
        make.right.mas_equalTo(-30);
    }];
    
    //脚标label
    UILabel *spotLab = [[UILabel alloc] init];
    spotLab.numberOfLines = 0;
    [self.view addSubview:spotLab];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"版权所有 Copyright 2019 国家电投内蒙古公司版权所有"attributes: @{NSFontAttributeName: [UIFont fontWithName:@"PingFangSC-Regular" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:209/255.0 green:234/255.0 blue:228/255.0 alpha:1.0]}];
    
    spotLab.attributedText = string;
    spotLab.textAlignment = NSTextAlignmentCenter;
    spotLab.alpha = 1.0;
    
    [spotLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-30);
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(153, 34));
    }];
}
#pragma mark - 登录
-(void)loginBtn:(UIButton *)sender {
    
    NSDictionary * params = @{
                              @"loginAccount": @"admin",
                              @"loginPwd":@"123456"
                              };
    
    [AFNRequest postWithURL:kLoginApp params:params success:^(id json) {
        if ([json[@"success"] intValue] == 1) {
            
            LoginMessage *loginMessage = [[LoginMessage alloc] init];
            [loginMessage setValuesForKeysWithDictionary:json[@"data"]];
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:loginMessage];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"UserMessage"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            [self setcookies];
            
             self.callBack(YES);
            
        } else {
            alertWithTitleMeaage(@"登录失败", json[@"text"]);
        }
        
    } failure:^(NSError *error) {
        
        
    }];
}

- (void)setcookies{
    NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL: [NSURL URLWithString:kLoginApp]];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cookies];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"kUserDefaultsCookie"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}




//弹出键盘UIView
-(void)transformView:(NSNotification*)aNSNotification{
    
//    //获取键盘弹出前的Rect
//    NSValue*keyBoardBeginBounds = [[aNSNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey];
//    CGRect beginRect = [keyBoardBeginBounds CGRectValue];
//
//    //获取键盘弹出后的Rect
//    NSValue *keyBoardEndBounds = [[aNSNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey];
//    CGRect endRect = [keyBoardEndBounds CGRectValue];
//
//    //获取键盘位置变化前后纵坐标Y的变化值
//    CGFloat deltaY = endRect.origin.y - beginRect.origin.y;
//    NSLog(@"看看这个变化的Y值:%f",deltaY);
    //在0.25s内完成self.view的Frame的变化，等于是给self.view添加一个向上移动deltaY的动画
    [UIView animateWithDuration:0.25f animations:^{

        self.view.frame = CGRectMake(0, -50, kScreenWidth, kScreenHeight);
    }];
    
}
//退出键盘UIView
-(void)keyboardWillHide:(NSNotification*)aNSNotification{
    
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationDuration:0.25];
    
    [UIView setAnimationCurve:7];
    
    self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    [UIView commitAnimations];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

@end
